package com.hung.triple.bootstrap;

import com.google.common.collect.Lists;
import com.hung.triple.bootstrap.config.AbstractTripleGlobalConfig;
import com.hung.triple.bootstrap.config.TripleRegistryConfig;
import com.hung.triple.common.enums.TripleRegisterType;
import com.hung.triple.common.enums.TripleRemoteProtocol;
import com.hung.triple.common.enums.TripleSerializerType;
import com.hung.triple.loadbalancer.RandomLoadBalancer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author Hung
 * @date 2022/11/27 9:45
 */
@Configuration(proxyBeanMethods = false)
public class TripleConsumerConfig extends AbstractTripleGlobalConfig {

    /**
     * 直连调用地址
     */
    protected String directUrl;
    /**
     * Consumer给Provider发心跳的间隔
     */
    protected int heartbeatPeriod;

    /**
     * Consumer给Provider重连的间隔
     */
    protected int reconnectPeriod;

    @Bean("tripleConsumerBootstrap")
    public TripleConsumerBootstrap initTriple() {
        // 1. 全局设置
        TripleConsumerConfig.setProtocol(TripleRemoteProtocol.TRIPLE_NETTY);
        TripleConsumerConfig.setTimeoutMS(2000);
        TripleConsumerConfig.setSerializerType(TripleSerializerType.PROTOBUF);
        TripleConsumerConfig.setLoadBalancer(RandomLoadBalancer.class);

        // 2. 注册中心设置
        TripleRegistryConfig registryConfig = new TripleRegistryConfig();
        registryConfig.setRegisterType(TripleRegisterType.NACOS);
        registryConfig.setAddressList(Lists.newArrayList("192.168.119.130:8848"));

        return new TripleConsumerBootstrap(registryConfig);
    }
}
